﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XDYRestaurant
{
    public partial class DishInfo : Form
    {
        private Context.Context ctx = new Context.Context();
        public DishInfo()
        {
            InitializeComponent();
        }
        //实现窗体的单例
        private static DishInfo _DishInfo;

        //对外提供实例化窗体方法
        public static DishInfo Create()
        {
            if(_DishInfo != null)
            {
                _DishInfo.Close();
            }
             _DishInfo = new DishInfo();
            return _DishInfo;
        }
        //显示DataGridView所有信息事件
        private void btnShowAllInfo_Click(object sender, EventArgs e)
        {
            var list = ctx.Cookbooks.Where(s => s.CookName.Contains(txtSearchName.Text)|| s.CookSpecies.Contains(txtSearchName.Text)).ToList();
            txtSearchName.Text = "";
            dgvDishInfoList.DataSource = list;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string name = txtName.Text.Trim().ToString();
            string type = txtType.Text.Trim().ToString();
            string id = txtId.Text.Trim().ToString();
            double f,price;
            byte t;//非负数 
            if (!byte.TryParse(txtId.Text.Trim(), out t))
            {
                MessageBox.Show("请输入正确的菜品号！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (id.Length != 6)
            {
                MessageBox.Show("请输入6位菜品号！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!double.TryParse(txtPrice.Text.Trim(), out f))
            {
                MessageBox.Show("请输入正确的价格！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                price = Convert.ToSingle(txtPrice.Text.Trim());
                price = Math.Round(price, 2);
            }
            Models.Cookbook cookbook = new Models.Cookbook();
            if (id.Equals("")) cookbook.CookId = "000000";
            else cookbook.CookId = id;
            //排除 “-00000”
            if(cookbook.CookId[0] == '-')
            {
                MessageBox.Show("请输入正确的价格！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            cookbook.CookName = name;
            cookbook.CookPrice = price;
            cookbook.CookSpecies = type;
            if(ctx.Cookbooks.FirstOrDefault(s => s.CookId == id) == null)
            {
                ctx.Cookbooks.Add(cookbook);
                ctx.SaveChanges();
                MessageBox.Show("添加成功!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSearchName.Text = "";
                var list = ctx.Cookbooks.Where(s => s.CookName.Contains(txtSearchName.Text) || s.CookSpecies.Contains(txtSearchName.Text)).ToList();               
                dgvDishInfoList.DataSource = list;
            }
            else
            {
                try
                {
                    DialogResult res = MessageBox.Show("确认要修改吗?", "提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (res != DialogResult.OK) return;
                    var cookbook2 = ctx.Cookbooks.FirstOrDefault(s => s.CookId == id);
                    cookbook2.CookName = name;
                    cookbook2.CookPrice = price;
                    cookbook2.CookSpecies = type;
                    ctx.SaveChanges();
                    MessageBox.Show("修改成功!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtSearchName.Text = "";
                    var list = ctx.Cookbooks.Where(s => s.CookName.Contains(txtSearchName.Text) || s.CookSpecies.Contains(txtSearchName.Text)).ToList();
                    dgvDishInfoList.DataSource = list;
                }
                catch
                {
                    MessageBox.Show("添加或修改失败!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void dgvDishInfoList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                txtId.Text = dgvDishInfoList.SelectedRows[0].Cells[0].Value.ToString();
                txtName.Text = dgvDishInfoList.SelectedRows[0].Cells[1].Value.ToString();
                if(dgvDishInfoList.SelectedRows[0].Cells[2].Value == null)
                {
                    txtType.Text = "";
                }
                else
                {
                    txtType.Text = dgvDishInfoList.SelectedRows[0].Cells[2].Value.ToString();
                }
                if(dgvDishInfoList.SelectedRows[0].Cells[3].Value == null)
                {
                    txtPrice.Text = "";
                }
                else
                {
                    txtPrice.Text = dgvDishInfoList.SelectedRows[0].Cells[3].Value.ToString();
                }
            }
            catch
            {
                //兼容性：如果选中非法就什么也不干
                return;
            }
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtId.Text = "";
            txtName.Text = "";
            txtType.Text = "";
            txtPrice.Text = "";
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string id = dgvDishInfoList.SelectedRows[0].Cells[0].Value.ToString();
                DialogResult res = MessageBox.Show("确认要删除吗?", "提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (res == DialogResult.OK)
                {
                    var cookbook = ctx.Cookbooks.FirstOrDefault(u => u.CookId == id); 
                    ctx.Cookbooks.Remove(cookbook); 
                    ctx.SaveChanges();
                    MessageBox.Show("删除成功!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtSearchName.Text = "";
                    var list = ctx.Cookbooks.Where(s => s.CookName.Contains(txtSearchName.Text) || s.CookSpecies.Contains(txtSearchName.Text)).ToList();
                    dgvDishInfoList.DataSource = list;
                }
            }
            catch
            {
                MessageBox.Show("该菜品有顾客正在用餐或曾销售过，如不再销售请将价格改为99999999!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
